package com.smallbug.longman.adapter;

import android.content.SharedPreferences;
import android.os.Build;
import android.speech.tts.TextToSpeech;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.smallbug.longman.R;
import com.smallbug.longman.ShowWord;
import com.smallbug.longman.bean.EXAMPLE;

import java.util.ArrayList;
import java.util.Locale;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by 17712 on 2017/11/6.
 */

public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHodler>{


    private final SharedPreferences settings;
    private  int upOrDown=0;
    private ArrayList<EXAMPLE> data;
    private ShowWord context;
    private TextToSpeech tts;

    public ExampleAdapter(ShowWord context, ArrayList<EXAMPLE> data,int upOrDown){
        this.data=data;
        this.context=context;
        this.upOrDown=upOrDown;
        settings = context.getSharedPreferences("settings", MODE_PRIVATE);
    }

    @Override
    public ExampleViewHodler onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=context.getLayoutInflater().inflate(R.layout.item_example,parent,false);
        return new ExampleViewHodler(view);
    }

    @Override
    public void onBindViewHolder(ExampleViewHodler holder, int position) {

        holder.base.setText(data.get(position).getBASE());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tts = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
                    @Override
                    public void onInit(int status) {
                        if (status == TextToSpeech.SUCCESS) {
                            tts.setLanguage(Locale.US);

                            String[] arr = data.get(position).getBASE().split(" ");
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                                tts.setSpeechRate(Float.parseFloat("1"));
                                tts.speak(data.get(position).getBASE(), TextToSpeech.QUEUE_ADD, null, null);
                            }else {
                                tts.setSpeechRate(Float.parseFloat("0.8"));
                                tts.speak(data.get(position).getBASE(), TextToSpeech.QUEUE_FLUSH, null);
                            }

                          /*  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
                            {
                                for(int start = 0; start < arr[0].length(); start++)
                                {
                                    tts.speak(arr[0].substring(start,  start + 1), TextToSpeech.QUEUE_ADD, null, null);
                                }
                                tts.setSpeechRate(1);
                                tts.speak(arr[0], TextToSpeech.QUEUE_ADD, null, null);
                            }*/
                            /*else
                            {
                                tts.speak(arr[0].substring(0, 1), TextToSpeech.QUEUE_FLUSH, null);
                            }*/

                        }
                    }
                });
            }
        });
        switch (upOrDown){
            case 0:
                break;
            case 1:
                holder.base.setTextSize(TypedValue.COMPLEX_UNIT_PX,  holder.base.getTextSize() + 5);
                settings.edit().putFloat("text_size", holder.base.getTextSize()).apply();
                break;
            case 2:
                holder.base.setTextSize(TypedValue.COMPLEX_UNIT_PX,  holder.base.getTextSize() - 5);
                settings.edit().putFloat("text_size", holder.base.getTextSize()).apply();
                break;
            default:
                break;
        }

    }

    @Override
    public int getItemCount() {
        return data==null?0:data.size();
    }


    public class ExampleViewHodler extends RecyclerView.ViewHolder{

        private final TextView base;
        private ImageView audio;

        public ExampleViewHodler(View itemView) {
            super(itemView);
            audio=(ImageView)itemView.findViewById(R.id.audio);
            base=(TextView)itemView.findViewById(R.id.base);
        }
    }
}
